package test;

/**
 * @author liuteng
 * @date 2017/8/2 15:34
 */
public class MyThreadLocal {
    ThreadLocal<Long> longThreadLocal = new ThreadLocal<Long>();
    ThreadLocal<String> stringThreadLocal = new ThreadLocal<String>();

    public void set() {
        longThreadLocal.set(Thread.currentThread().getId());
        stringThreadLocal.set(Thread.currentThread().getName());
    }

    public long getLong() {
        return longThreadLocal.get();
    }

    public String getString() {
        return stringThreadLocal.get();
    }

    public static void main(String[] args) throws InterruptedException {
        final MyThreadLocal myThreadLocal = new MyThreadLocal();
        myThreadLocal.set();
        System.out.println(myThreadLocal.getLong());
        System.out.println(myThreadLocal.getString());
        Thread thread = new Thread() {
            public void run() {
                myThreadLocal.set();
                System.out.println(myThreadLocal.getLong());
                System.out.println(myThreadLocal.getString());
            }
        };
        thread.start();
        thread.join();

        System.out.println(myThreadLocal.getLong());
        System.out.println(myThreadLocal.getString());

    }
}
